<template>
  <div class="app-container">
    <el-form :inline="true" size="small" label-width="100">
      <el-form-item>
        <el-button size="small" class="btn-add" @click="handleSelectProduct()" style="margin-left: 20px">添加</el-button>
      </el-form-item>
    </el-form>
    <div class="table-container">
      <el-table ref="productRelationTable" height="100%" :data="list" style="width: 100%" v-loading="listLoading" border>
        <el-table-column label="编号" width="80" align="center">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="商品名称" minWidth="200" show-overflow-tooltip align="center">
          <template #default="scope">{{ scope.row.product.name }}</template>
        </el-table-column>
        <el-table-column label="货号" width="140" align="center">
          <template #default="scope">NO.{{ scope.row.product.productSn }}</template>
        </el-table-column>
        <el-table-column label="商品价格(元)" width="120" align="center">
          <template #default="scope">{{ scope.row.product.price }}</template>
        </el-table-column>
        <el-table-column label="剩余数量" width="100" align="center">
          <template #default="scope">{{ scope.row.product.stock }}</template>
        </el-table-column>
        <el-table-column label="秒杀价格(元)" width="120" align="center">
          <template #default="scope">
            <p v-if="scope.row.flashPromotionPrice !== null">{{ scope.row.flashPromotionPrice }}</p>
          </template>
        </el-table-column>
        <el-table-column label="秒杀数量" width="100" align="center">
          <template #default="scope">{{ scope.row.flashPromotionCount }}</template>
        </el-table-column>
        <el-table-column label="限购数量" width="100" align="center">
          <template #default="scope">{{ scope.row.flashPromotionLimit }}</template>
        </el-table-column>
        <el-table-column label="排序" width="100" align="center">
          <template #default="scope">{{ scope.row.sort }}</template>
        </el-table-column>
        <el-table-column label="操作" width="100" align="center">
          <template #default="scope">
            <el-button size="small" link @click="handleUpdate(scope.row)">编辑 </el-button>
            <el-button size="small" link type="danger" @click="handleDelete(scope.row)">删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        v-model:current-page="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog title="选择商品" v-model="selectDialogVisible" width="50%">
      <el-input v-model="productSearch.keyword" style="width: 250px; margin-bottom: 20px" size="small" placeholder="商品名称搜索">
        <template #append>
          <el-button :icon="Search" @click="handleSelectSearch()"></el-button>
        </template>
      </el-input>
      <el-table :data="productList" height="100%" @selection-change="handleDialogSelectionChange" border>
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="商品名称" minWidth="250" show-overflow-tooltip align="center">
          <template #default="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="货号" width="160" align="center">
          <template #default="scope">NO.{{ scope.row.productSn }}</template>
        </el-table-column>
        <el-table-column label="价格(元)" width="120" align="center">
          <template #default="scope">{{ scope.row.price }}</template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          background
          @size-change="handleDialogSizeChange"
          @current-change="handleDialogCurrentChange"
          layout="prev, pager, next"
          v-model:current-page="productSearch.pageNum"
          :page-size="productSearch.pageSize"
          :page-sizes="[5, 10, 15]"
          :total="productTotal"
        >
        </el-pagination>
      </div>
      <div style="clear: both"></div>
      <template #footer>
        <el-button size="small" @click="selectDialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="handleSelectDialogConfirm()">确 定</el-button>
      </template>
    </el-dialog>
    <el-dialog title="编辑秒杀商品信息" v-model="editDialogVisible" width="40%">
      <el-form :model="flashProductRelation" ref="flashProductRelationForm" label-width="auto" size="small">
        <el-form-item label="商品名称：">
          <el-input v-model="flashProductRelation.product.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="货号：">
          <el-input v-model="flashProductRelation.product.productSn" disabled> </el-input>
        </el-form-item>
        <el-form-item label="商品价格：">
          <el-input v-model="flashProductRelation.product.price" disabled>
            <template #append>元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="秒杀价格：">
          <el-input v-model="flashProductRelation.flashPromotionPrice">
            <template #append>元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="剩余数量：">
          <el-input v-model="flashProductRelation.product.stock" disabled></el-input>
        </el-form-item>
        <el-form-item label="秒杀数量：">
          <el-input v-model="flashProductRelation.flashPromotionCount"></el-input>
        </el-form-item>
        <el-form-item label="限购数量：">
          <el-input v-model="flashProductRelation.flashPromotionLimit"></el-input>
        </el-form-item>
        <el-form-item label="排序：">
          <el-input v-model="flashProductRelation.sort"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="editDialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleEditDialogConfirm()" size="small">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { fetchList, createFlashProductRelation, deleteFlashProductRelation, updateFlashProductRelation } from '@/api/flashProductRelation'
import { fetchList as fetchProductList } from '@/api/product'
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search } from '@element-plus/icons-vue'
import type { FlashProductRelation, FlashProductRelationPageDto } from '@/models/flashProductRelation'
import type { Product, ProductPageParams } from '@/models/product'

defineOptions({ name: 'FlashPromotionProductRelationList' })
const route = useRoute()
const listQuery = ref<FlashProductRelationPageDto>({ pageNum: 1, pageSize: 10 })
const list = ref<FlashProductRelation[]>([])
const total = ref(0)
const listLoading = ref(false)
const selectDialogVisible = ref(false)
const editDialogVisible = ref(false)
const flashProductRelation = ref<FlashProductRelation>({})

const productSearch = ref<ProductPageParams>({ pageNum: 1, pageSize: 10 })
const productList = ref<Product[]>([])
const productTotal = ref(0)
const productSelectList = ref<Product[]>([])
onMounted(() => {
  listQuery.value.flashPromotionId = Number(route.query.flashPromotionId)
  listQuery.value.flashPromotionSessionId = Number(route.query.flashPromotionSessionId)
  getList()
})

function handleSizeChange(val: number) {
  listQuery.value.pageNum = 1
  listQuery.value.pageSize = val
  getList()
}

function handleCurrentChange(val: number) {
  listQuery.value.pageNum = val
  getList()
}

function handleSelectProduct() {
  selectDialogVisible.value = true
  getDialogList()
}

function handleUpdate(row: FlashProductRelation) {
  editDialogVisible.value = true
  flashProductRelation.value = row
}

async function handleDelete(row: FlashProductRelation) {
  await ElMessageBox.confirm('是否要删除该商品?', '提示', { type: 'warning' })
  await deleteFlashProductRelation(row.id as number)
  ElMessage.success('删除成功')
  getList()
}

function handleSelectSearch() {
  getDialogList()
}

function handleDialogSizeChange(val: number) {
  productSearch.value.pageNum = 1
  productSearch.value.pageSize = val
  getDialogList()
}

function handleDialogCurrentChange(val: number) {
  productSearch.value.pageNum = val
  getDialogList()
}

function handleDialogSelectionChange(val: Product[]) {
  productSelectList.value = val
}

async function handleSelectDialogConfirm() {
  if (productSelectList.value.length < 1) {
    ElMessage.warning('请选择一条记录')
    return
  }
  const selectProducts = productSelectList.value.map((item) => ({
    productId: item.id,
    flashPromotionId: listQuery.value.flashPromotionId,
    flashPromotionSessionId: listQuery.value.flashPromotionSessionId,
  }))
  await ElMessageBox.confirm('使用要进行添加操作?', '提示', { type: 'warning' })
  await createFlashProductRelation(selectProducts)
  selectDialogVisible.value = false
  productSelectList.value = []
  getList()
  ElMessage.success('添加成功')
}

async function handleEditDialogConfirm() {
  await updateFlashProductRelation(flashProductRelation.value)
  ElMessage.success('修改成功')
  editDialogVisible.value = false
  getList()
}

async function getList() {
  listLoading.value = true
  try {
    const response = await fetchList(listQuery.value)
    list.value = response.data.data.list
    total.value = response.data.data.total
  } finally {
    listLoading.value = false
  }
}

async function getDialogList() {
  const response = await fetchProductList(productSearch.value)
  productList.value = response.data.data.list
  productTotal.value = response.data.data.total
}
</script>
<style scoped>
.operate-container {
  margin-top: 0;
}

.input-width {
  width: 200px;
}
</style>
